<?php


namespace app\model;
use think\facade\Db;
use think\Model;

class Address extends Model
{
    public function getAll($id){
        $res = Address::where('user_id',$id)->select();
        return $res;
    }

    public function addAdd($data){
        if ($data['is_default'] == 1){
            Db::execute("UPDATE shop_address SET is_default = 0 ");
            $res =$res = Address::create($data);

        }else{
            $res =$res = Address::create($data);

        }
        return $res;
    }

    public function getOne($id){
        $res = Address::where('id',$id)->find();
        return $res;
    }

    public function updateAddr($data){
        if ($data['is_default'] == 1){
//            $res = Address::where('id',$data['id'],'!=')->field('is_default')->save(1);
            Db::execute("UPDATE shop_address SET is_default = 0 WHERE id != ".$data['id']);
            $res = Address::where('id',$data['id'])->update($data);

        }else{
            $res = Address::where('id',$data['id'])->update($data);

        }
        return $res;
    }

    public function delOne($id){
        $res = Address::where('id',$id)->delete();
        return $res;
    }

    public function upDefault($id){
        $data['is_default'] = 1;
//        $res = Address::where('id',$id)->field('is_default')->save();
        Db::execute("UPDATE shop_address SET is_default = 0 WHERE id != ".$id);

        $res = Db::execute("UPDATE shop_address SET is_default = 1 WHERE id = ".$id);

        return $res;
    }

}